<?php
/**
 * Author: Hancock
 * Email: 84622365@qq.com
 * Date: 7/18/2016
 * Des: 系统管理员
 */
defined('INHkMvc') or exit('Access Invalid!');

class App_orderModel extends AppModel
{    
    public function editOrderList($datas,$member_id){
    	    $res=$this->find(['order_number'=>$datas['order_number']]);
    	    $level=M('app.member_vip_level')->find(['level'=>$datas['level']]);
    	    if($res===false || empty($res)){
    	      $order['order_number']=$datas['order_number'];
    	      $order['status']=$datas['order_status'];
    	      $order['member_id']=$member_id;
    	      if ($datas['balance_money']<=0) {
    	          $order['point'] = round($datas['pay_money'] * $datas['tk_rate']*$level['fee']*$level['vip_tk_rate']*$datas['divide_rate']/10000, 2);
    	          $order['pay_money']=$datas['pay_money'];
    	      } else {
    	          $order['point'] = round($datas['balance_money'] * $datas['tk_rate']*$level['fee']*$level['vip_tk_rate']*$datas['divide_rate']/10000, 2);
    	          $order['pay_money']=$datas['balance_money'];
    	      }
    	      $result=$this->insert($order);  
    	    }else{
    	      if($res['status']==0){
    	         $order['status']=$datas['order_status'];  
    	      }else if($res['status']==1){
    	         if($datas['order_status']==0){
    	            $order['status']=$res['status']; 
    	         }else{
    	            $order['status']=$datas['order_status'];
    	         }
    	      }else{
    	         $order['status']=$res['status'];
    	      }
    	      if ($datas['balance_money']<=0) {
    	          $order['point'] = round($datas['pay_money'] * $datas['tk_rate']*$level['fee']*$level['vip_tk_rate']*$datas['divide_rate']/10000, 2)+$res['point'];
    	          $order['pay_money']=$datas['pay_money']+$res['pay_money'];
    	      } else {
    	          $order['point'] = round($datas['balance_money'] * $datas['tk_rate']*$level['fee']*$level['vip_tk_rate']*$datas['divide_rate']/10000, 2)+$res['point'];
    	          $order['pay_money']=$datas['balance_money']+$res['pay_money'];
    	      }
    	       $result=$this->update($order,['id'=>$res['id']]);
    	    }
    	    if($result===false){
               return false;
    	    }else{
               return true;
    	    }
    }
    public function updateOrderList($datas){
    	$res=$this->find(['order_number'=>$datas['order_number']]);
    	if(!empty($res)){
    	  $level=M('app.member_vip_level')->find(['level'=>$datas['level']]);
    	  if($res['status']==0){
    	     $order['status']=$datas['rights_status'];  
    	  }else if($res['status']==1){
    	     if($datas['rights_status']==0){
    	        $order['status']=$res['status']; 
    	     }else{
    	        $order['status']=$datas['rights_status'];
    	     }
    	  }else{
    	     $order['status']=$res['status'];
    	  }
    	  if ($datas['rights_tk_rate']>0) {
    	      $order['point'] =$res['point']-round($datas['rights_money'] * $datas['rights_tk_rate']*$level['fee']*$level['vip_tk_rate']/100, 2);
    	  }
    	  $order['pay_money']=$res['pay_money']-$datas['rights_money'];
    	  $result=$this->update($order,['id'=>$res['id']]);
    	  if($result===false){
               return false;
    	  }
    	}
    	return true;
    }
}
